﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using CubicFWNet.Common;
using CubicFWNet.Application.SystemManager;
using CubicFWNet.Data.Models;
using CubicFWNet.Application;

namespace CubicFWNet.Mvc.Controllers
{
    public class LoginController : Controller
    {
        private ModuleApp _moduleApp;

        public LoginController()
        {
            _moduleApp = new ModuleApp();
        }

        [HttpGet]
        public ViewResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult GetAuthCode()
        {
            return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
        }

        [HttpGet]
        public ActionResult OutLogin()
        {
            if (OperatorProvider.Provider.GetCurrent() != null)
            {
                new LogApp().WriteDbLog(new LogInLog
                {
                    Type = "登出",
                    UserId = OperatorProvider.Provider.GetCurrent().UserId,
                    UserName = OperatorProvider.Provider.GetCurrent().UserName,
                    RealName = OperatorProvider.Provider.GetCurrent().FamilyName,
                });
                Session.Abandon();
                Session.Clear();
                OperatorProvider.Provider.RemoveCurrent();
            }
            return RedirectToAction("Index", "Login");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult CheckLogin(string username, string password, string code)
        {
            LogInLog logEntity = new LogInLog();
            logEntity.Type = "登录";
            try
            {
                if (Session["cubic_session_verifycode"].IsEmpty() || Md5.md5(code.ToLower(), 16) != Session["cubic_session_verifycode"].ToString())
                {
                    throw new Exception("验证码错误，请重新输入");
                }

                User user = new UserApp().CheckLogin(username, password);
                if (user != null)
                {
                    OperatorModel operatorModel = new OperatorModel();
                    operatorModel.UserId = user.Id;
                    operatorModel.UserName = user.UserName;
                    operatorModel.FamilyName = user.RealName;
                    //operatorModel.ShortName = user.ShortName.Trim();
                    operatorModel.LoginIPAddress = Net.Ip;
                    operatorModel.LoginIPAddressName = Net.GetLocation(operatorModel.LoginIPAddress);
                    operatorModel.LoginTime = DateTime.Now;
                    operatorModel.LoginToken = DESEncrypt.Encrypt(Guid.NewGuid().ToString());

                    //operatorModel.UserModules = _moduleApp.LoadForUser(operatorModel.UserId, operatorModel.IsSystem);

                    if (user.UserName == "admin")
                    {
                        operatorModel.IsSystem = true;
                    }
                    else
                    {
                        operatorModel.IsSystem = false;
                    }
                    // operatorModel.Modules = _moduleApp.LoadForUser(user);

                    OperatorProvider.Provider.AddCurrent(operatorModel);
                    logEntity.UserId = user.Id;
                    logEntity.UserName = username;
                    logEntity.RealName = user.RealName;
                    logEntity.Type = "登录";
                    new LogApp().WriteDbLog(logEntity);
                }
                return Content(new AjaxResult { state = ResultType.success.ToString(), message = "登录成功。" }.ToJson());
            }
            catch (Exception ex)
            {
                logEntity.UserName = username;
                logEntity.RealName = username;
                logEntity.Type = "登录失败";
                new LogApp().WriteDbLog(logEntity);
                return Content(new AjaxResult { state = ResultType.error.ToString(), message = ex.Message }.ToJson());
            }
        }

    }
}